var express = require('express');
var router = express.Router();
const {openAiRequest} = require('./components/axios-model.js')
const {deviceObj} = require('../utils/model/device_info')
const {deviceMsgObj} = require('../utils/model/device_message')
const {successData, errorData} = require("../utils/response");
/**
 * 获得某个用户
 * @api {GET} /users/index 获得某个用户
 * @apiName getUser
 * @apiGroup users
 */
router.get('/index', function (req, res, next) {
  res.send('respond with a resource');
});

router.get('/getOpenAiCount', function (req, res, next) {
  let url = "https://api.openai.com/v1/dashboard/billing/subscription"
  openAiRequest.getData({url}).then(result => {
    return successData(res, result.data)
  }).catch(err => {
    return errorData(res, err)
  })
});

router.get('/getFiles', async function (req, res) {
  const uuid = req.query.uuid, url = 'https://api.openai.com/v1/files'
  openAiRequest.getData({url}).then(result => {
    return successData(res, result.data)
  }).catch(err => {
    return errorData(res, err)
  })
})

router.get('/getModels', async function (req, res) {
  let uuid = req.query.uuid, url = 'https://api.openai.com/v1/models'
  openAiRequest.getData({url}).then(result => {
    return successData(res, result.data)
  }).catch(err => {
    return errorData(res, err)
  })
})

router.get('/getDevice', async function (req, res) {
  const uuid = req.query.uuid
  if (!uuid) return errorData(res, "没有参数uuid！")
  let info = await deviceObj.getDeviceInfo({uuid})
  return successData(res, info)
})

router.get('/resetCount', async function (req, res) {
  const uuid = req.query.uuid
  if (!uuid) return errorData(res, "没有参数uuid！")
  let info = await deviceObj.updateDevice({uuid, use_count: 5})
  return successData(res, info)
})

router.post('/chat', async function (req, res) {
  let url = 'https://api.openai.com/v1/chat/completions'
  const uuid = req.body.uuid, messages = req.body.messages,
      platform = req.body.platform || '', ip_info = req.body.ip_info || req.ip;
  if (!messages || !messages.length) return errorData(res, "参数messages错误！")
  let body = {model: "gpt-3.5-turbo", messages: messages};
  let haveCount = await deviceObj.haveCount({uuid, platform, ip_info})
  if (haveCount <= 0) return errorData(res, "50次试用次数已经用完！")
  openAiRequest.getData({method: 'POST', url, body: body}).then(async result => {
    let aiMsgObj, {choices} = result.data
    if (choices && choices.length) aiMsgObj = choices[0].message
    let mMsgObj = messages[messages.length - 1]
    await deviceMsgObj.addMessage({device_id: uuid, role: mMsgObj.role, content: mMsgObj.content})
    await deviceMsgObj.addMessage({device_id: uuid, role: aiMsgObj.role, content: aiMsgObj.content})
    await useDeviceCount({uuid, platform, ip_info, res})
    return successData(res, result.data)
  }).catch(err => {
    return errorData(res, err.sql || err.name || err)
  })
})

//使用设备id的数量
async function useDeviceCount({uuid, platform = '', ip_info = '', res}) {
  const result = await deviceObj.useCount({uuid, platform, ip_info})
  if (!result.data) errorData(res, result.msg)
  return result.data
}

router.get('/getMessageList', async function (req, res) {
  const uuid = req.query.uuid
  if (!uuid) return errorData(res, "没有参数uuid！")
  const result = await deviceMsgObj.getMessageList({device_id: uuid})
  return successData(res, result)
})

router.get('/getAllMessage', async function (req, res) {
  let {pageNum, pageSize, device_id, dateTime, content, role} = req.query
  if (!pageSize) pageSize = 10
  if (!pageNum) pageNum = 0
  const result = await deviceMsgObj.getAllMessageList({
    content, device_id, role, dateTime,
    offset: Number(pageNum), limit: Number(pageSize)
  })
  return successData(res, result)
})

module.exports = router;
